<template>
  <ul class="searchresult">
    <template v-if="songs.length != 0">
      <li v-for="(item) in songs" :key="item.id">
        <div class="left">
          <h5 v-html="$options.filters.highlight(item.name,keywords)"></h5>
          <p>
            <span v-if="item.artists">
              <span v-html="$options.filters.highlight(item.artists[0].name,keywords)"></span>
            </span> - 
            <span v-html="$options.filters.highlight(item.name,keywords)"></span>
          </p>
        </div>
        <div class="right">
          <img @click="$emit('play',item)" class="playBtn" src="../assets/play.png" />
        </div>
      </li>
    </template>
  </ul>
</template>

<script>
export default {
  name: "SearchResult",
  props: ["keywords"],
  data() {
    return {
      songs: [],
    };
  },
  filters: {
    highlight(value,keywords) {
      return String(value).replace(keywords,'<span style="color:red">' + keywords + "</span>");
    },
  },
  created() {
    this.$http
      .get("/search/suggest", {
        params: {
          keywords: this.keywords,
        },
      })
      .then((d) => {
        if (!d.data.result.songs) return;
        this.songs = d.data.result.songs;
        // console.log(d);
        // this.songs = d.data.result.songs;
      });
  },
};
</script>

<style lang="less" scoped>
.searchresult {
  padding-left: 10px;
  li {
    display: flex;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    padding: 6px 0px;
    i.num {
      display: inline-block;
      line-height: 43px;
      font-style: normal;
      width: 40px;
      font-size: 17px;
    }
    i.red {
      color: #df3436;
    }
    .left {
      flex: 1;
      h5 {
        font-size: 17px;
        font-weight: 400;
      }
      p {
        font-size: 12px;
        color: #888;
      }
    }
    .right {
      width: 25px;
      height: 25px;
      padding: 0px 14px;
      display: flex;

      .playBtn {
        display: inline-block;
        width: 25px;
        height: 25px;
        margin-top: 10px;
      }
    }
  }
}
</style>